﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace LowLevelGraphics.Filter
{
    public class Merge : BaseImageFilter
    {
        protected Dictionary<UnsafeBitmap, Rectangle> m_aBitmap = new Dictionary<UnsafeBitmap,Rectangle>();
        protected bool m_b = false;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_aBitmap"></param>
        public Merge(Dictionary<UnsafeBitmap, Rectangle> _aBitmap)
        {
            m_aBitmap = _aBitmap;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_merge"></param>
        internal Merge(Merge _merge)
        {

        }

        /// <summary>
        /// gets or sets Adjust Rectangle
        /// </summary>
        public bool AdjustRectangleSize
        {
            get { return m_b; }
            set { m_b = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            _bitmap.Dispose();
            Graphics g = Graphics.FromImage(_bitmap.InternalBitmap);
            Rectangle rectangle = Rectangle.Empty;

            if (m_b)
            {
                foreach (KeyValuePair<UnsafeBitmap, Rectangle> keyValuePair in m_aBitmap)
                {
                    rectangle = Rectangle.Union(rectangle, keyValuePair.Value);
                }
            }

            foreach (KeyValuePair<UnsafeBitmap, Rectangle> keyValuePair in m_aBitmap)
            {
                g.DrawImage(keyValuePair.Key.InternalBitmap, keyValuePair.Value);
            }
            _bitmap.LockImage();
            return _bitmap;
        }

        /// <summary>
        /// Clones this instance
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Merge(this);
        }
    }
}
